add support for shape files with non us-ascii filenames. (#148)
authortsteven4 <tsteven4@users.noreply.github.com>
Wed, 13 Dec 2017 18:31:16 +0000 (11:31 -0700)
committerGitHub <noreply@github.com>
Wed, 13 Dec 2017 18:31:16 +0000 (11:31 -0700)
shape.cc
test_encoding_latin1
test_encoding_utf8

index 32282b96e0d6327407908b440dc200ffdbd028d5..5f4fde403fa796124c46d547b8d6895b9d55a88c 100644 (file)
--- a/shape.cc
+++ b/shape.cc
@@ -59,6 +59,107 @@ arglist_t shp_args[] = {
   ARG_TERMINATOR
 };
 
+
+/************************************************************************/
+/*                              SHPOpenGpsbabel()                       */
+/************************************************************************/
+
+SHPHandle SHPAPI_CALL
+SHPOpenGpsbabel( const QString& pszLayer, const char * pszAccess )
+
+{
+    SAHooks sHooks;
+
+#ifdef SHPAPI_UTF8_HOOKS
+    SASetupUtf8Hooks( &sHooks );
+    return SHPOpenLL( pszLayer.toUtf8().constData(), pszAccess, &sHooks );
+#else
+    SASetupDefaultHooks( &sHooks );
+    return SHPOpenLL( qPrintable(pszLayer), pszAccess, &sHooks );
+#endif
+
+}
+
+/************************************************************************/
+/*                             SHPCreateGpsbabel()                      */
+/*                                                                      */
+/*      Create a new shape file and return a handle to the open         */
+/*      shape file with read/write access.                              */
+/************************************************************************/
+
+SHPHandle SHPAPI_CALL
+SHPCreateGpsbabel( const QString& pszLayer, int nShapeType )
+
+{
+    SAHooks sHooks;
+
+#ifdef SHPAPI_UTF8_HOOKS
+    SASetupUtf8Hooks( &sHooks );
+    return SHPCreateLL( pszLayer.toUtf8().constData(), nShapeType, &sHooks );
+#else
+    SASetupDefaultHooks( &sHooks );
+    return SHPCreateLL( qPrintable(pszLayer), nShapeType, &sHooks );
+#endif
+
+}
+
+/************************************************************************/
+/*                              DBFOpenGpsbabel()                       */
+/*                                                                      */
+/*      Open a .dbf file.                                               */
+/************************************************************************/
+   
+DBFHandle SHPAPI_CALL
+DBFOpenGpsbabel( const QString& pszFilename, const char * pszAccess )
+
+{
+    SAHooks sHooks;
+
+#ifdef SHPAPI_UTF8_HOOKS
+    SASetupUtf8Hooks( &sHooks );
+    return DBFOpenLL( pszFilename.toUtf8().constData(), pszAccess, &sHooks );
+#else
+    SASetupDefaultHooks( &sHooks );
+    return DBFOpenLL( qPrintable(pszFilename), pszAccess, &sHooks );
+#endif
+
+}
+
+/************************************************************************/
+/*                            DBFCreateExGpsbabel()                     */
+/*                                                                      */
+/*      Create a new .dbf file.                                         */
+/************************************************************************/
+
+DBFHandle SHPAPI_CALL
+DBFCreateExGpsbabel( const QString& pszFilename, const char* pszCodePage )
+
+{
+    SAHooks sHooks;
+
+#ifdef SHPAPI_UTF8_HOOKS
+    SASetupUtf8Hooks( &sHooks );
+    return DBFCreateLL( pszFilename.toUtf8().constData(), pszCodePage , &sHooks );
+#else
+    SASetupDefaultHooks( &sHooks );
+    return DBFCreateLL( qPrintable(pszFilename), pszCodePage , &sHooks );
+#endif
+
+}
+
+/************************************************************************/
+/*                             DBFCreateGpsbabel()                      */
+/*                                                                      */
+/* Create a new .dbf file with default code page LDID/87 (0x57)         */
+/************************************************************************/
+
+DBFHandle SHPAPI_CALL
+DBFCreateGpsbabel( const QString& pszFilename )
+
+{
+    return DBFCreateExGpsbabel( pszFilename, "LDID/87" ); // 0x57
+}
+
 static
 void dump_fields(void)
 {
@@ -99,12 +200,12 @@ my_rd_init(const QString& fname)
 {
   ifname = fname;
   // TODO: The .prj file can define the the coordinate system and projection information.
-  ihandle = SHPOpen(qPrintable(fname), "rb");
+  ihandle = SHPOpenGpsbabel(fname, "rb");
   if (ihandle == NULL) {
     fatal(MYNAME ": Cannot open shp file %s for reading\n", qPrintable(ifname));
   }
 
-  ihandledb = DBFOpen(qPrintable(fname), "rb");
+  ihandledb = DBFOpenGpsbabel(fname, "rb");
   if (ihandledb == NULL) {
     fatal(MYNAME ": Cannot open dbf file %s for reading\n", qPrintable(ifname));
   }
@@ -376,13 +477,13 @@ my_write(void)
   switch (global_opts.objective) {
   case wptdata:
   case unknown_gpsdata:
-    ohandle = SHPCreate(qPrintable(ofname), SHPT_POINT);
+    ohandle = SHPCreateGpsbabel(ofname, SHPT_POINT);
 
     if (ohandle == NULL) {
       fatal(MYNAME ": Cannot open shp file %s for writing\n",
             qPrintable(ofname));
     }
-    ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n");
+    ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n");
     if (ohandledb == NULL) {
       fatal(MYNAME ": Cannot open dbf file %s for writing\n",
             qPrintable(ofname));
@@ -392,13 +493,13 @@ my_write(void)
     break;
   case rtedata:
   case trkdata:
-    ohandle = SHPCreate(qPrintable(ofname), SHPT_ARC);
+    ohandle = SHPCreateGpsbabel(ofname, SHPT_ARC);
 
     if (ohandle == NULL) {
       fatal(MYNAME ": Cannot open shp file %s for writing\n",
             qPrintable(ofname));
     }
-    ohandledb = DBFCreateEx(qPrintable(ofname), "UTF-8\n");
+    ohandledb = DBFCreateExGpsbabel(ofname, "UTF-8\n");
     if (ohandledb == NULL) {
       fatal(MYNAME ": Cannot open dbf file %s for writing\n",
             qPrintable(ofname));
index 360e60e6757a01bd7a5caa72ba8a5376eca9573d..e2aa1c493da8085288ccb3c62706dfb88c642e6f 100755 (executable)
@@ -74,8 +74,42 @@ filenamebase=test_encoding_file
     errorcount=`expr $errorcount + 1`
   fi
 
+# test input file name mangling with shape files
+  echo "testing input file name with shape files"
+  rm -f ${TMPDIR}/test_encoding_file*
+  for ext in cpg dbf prj shp shx
+  do
+    cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext}
+  done
+  ${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || {
+    echo "ERROR: The input file name was mangled."
+    errorcount=`expr $errorcount + 1`
+  }
+
+# test output file name mangling with shape files
+  echo "testing output file name with shape files"
+  rm -f ${TMPDIR}/test_encoding_file*
+  ${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
+  for ext in cpg dbf shp shx
+  do
+    count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
+    if [ $count -lt 1 ]; then
+      echo "ERROR: The output file name was mangled."
+      errorcount=`expr $errorcount + 1`
+    fi
+  done
+  rm -f ${TMPDIR}/test_encoding_file*
+  ${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
+  for ext in cpg dbf shp shx
+  do
+    count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
+    if [ $count -lt 1 ]; then
+      echo "ERROR: The output file name was mangled."
+      errorcount=`expr $errorcount + 1`
+    fi
+  done
+
 # TODO: add tests to cover formats that use other open methods.
-#       for example shape files.
 
 else
   echo "$0 cannot run without the en_US.iso88591 locale."
index 8abe07a66c5870db64fd7799cccc15a4ba60cedf..055538df0e0c2dbd666cac1b19500bf7c811a0eb 100755 (executable)
@@ -74,8 +74,42 @@ filenamebase=test_encoding_fileαβγ
     errorcount=`expr $errorcount + 1`
   fi
 
+# test input file name mangling with shape files
+  echo "testing input file name with shape files"
+  rm -f ${TMPDIR}/test_encoding_file*
+  for ext in cpg dbf prj shp shx
+  do
+    cp ${REFERENCE}/gis.osm_railways_free_1.${ext} ${TMPDIR}/${filenamebase}.${ext}
+  done
+  ${PNAME} -r -i shape -f ${TMPDIR}/${filenamebase}.shp -o kml -F ${TMPDIR}/test_encoding_fileo.kml || {
+    echo "ERROR: The input file name was mangled."
+    errorcount=`expr $errorcount + 1`
+  }
+
+# test output file name mangling with shape files
+  echo "testing output file name with shape files"
+  rm -f ${TMPDIR}/test_encoding_file*
+  ${PNAME} -r -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
+  for ext in cpg dbf shp shx
+  do
+    count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
+    if [ $count -lt 1 ]; then
+      echo "ERROR: The output file name was mangled."
+      errorcount=`expr $errorcount + 1`
+    fi
+  done
+  rm -f ${TMPDIR}/test_encoding_file*
+  ${PNAME} -w -i gpx -f ${REFERENCE}/bounds-test.gpx -o shape -F ${TMPDIR}/${filenamebase}.shp
+  for ext in cpg dbf shp shx
+  do
+    count=$(ls -1 -l ${TMPDIR}/${filenamebase}.${ext} 2>/dev/null | wc -l)
+    if [ $count -lt 1 ]; then
+      echo "ERROR: The output file name was mangled."
+      errorcount=`expr $errorcount + 1`
+    fi
+  done
+
 # TODO: add tests to cover formats that use other open methods.
-#       for example shape files.
 
 else
   echo "$0 cannot run without the UTF-8 charmap."